home *** CD-ROM | disk | FTP | other *** search
- Path: hobbes.ece.uiuc.edu!user
- From: yang@ripley.ece.uiuc.edu (Weimin Yang)
- Newsgroups: comp.lang.c++
- Subject: Re: Need help with overloading "<<"
- Date: Sun, 21 Jan 1996 21:17:31 +0700
- Organization: UIUC
- Message-ID: <yang-2101962117310001@hobbes.ece.uiuc.edu>
- References: <4duet0$ju9@wegener.ems.psu.edu>
- NNTP-Posting-Host: hobbes.ece.uiuc.edu
-
- In article <4duet0$ju9@wegener.ems.psu.edu>, mahesh@cerse.psu.edu wrote:
-
- >Hi !
- >
- >I was trying to overload the "<<" operator for a Complex class,
- >without much sucess. Could someone please help me out ?
- >
- >In the main program, when I do
- >
- >cout << a << endl ; ( here a is defined as Complex a(1.0, 1.0) ;)
- >
- >the compiler gives the following error :
- >
- >"prog2.C", line 11.21: 1540-070: (S) Call does not match any argument list
- >
- >for "ostream::operator<<".
- >
- >
- >The overloading function is :
- >
- >ostream& operator<<(ostream& s, Complex& z)
- >{
- > s << z.real() << " + " << z.img() <<"i" << endl;
- > return s;
- >}
- >
- >
- >and the class definition is :
- >
- >// complex1.h
- >
- >class Complex
- >{
- > private:
- >
- > double real_;
- > double img_;
- >
- > public:
- >
- > Complex(); // Constuctors
- > Complex(double real, double img);
- >
- > Complex operator+ (Complex& z); // operations
- >
- > double real() {return real_;}
- > double img() {return img_ ;}
- >
- > void cprint();
- >
- > ~Complex() ; // destructor
- >
- >} ;
- >
- >
- >I have been stuck with this for some time now...
- >
- >
- >Thanks a lot
- >
- >Mahesh
-
-
-
- you should put one line in your class definition:
-
- friend ostream& operator<<(ostream& s, Complex& z);
-